import tkinter as tk
from random import randint, choice


class BubbleShooter:
    def __init__(self, root):
        self.root = root
        self.root.title("泡泡解压小游戏")

        # 游戏参数设置
        self.score = 0
        self.time_left = 30
        self.bubble_speed = 3
        self.max_bubbles = 15

        # 创建游戏画布
        self.canvas = tk.Canvas(root, width=800, height=600, bg='lightblue')
        self.canvas.pack()

        # 创建计分板
        self.score_label = tk.Label(root, text=f"分数: {self.score}", font=('Arial', 16))
        self.score_label.pack(side=tk.LEFT)

        # 创建计时器
        self.time_label = tk.Label(root, text=f"剩余时间: {self.time_left}", font=('Arial', 16))
        self.time_label.pack(side=tk.RIGHT)

        # 开始游戏
        self.create_bubbles()
        self.update_timer()

    def create_bubbles(self):
        """生成随机泡泡"""
        colors = ['#FFB3BA', '#BAFFC9', '#BAE1FF', '#FFDFBA', '#BAB6FF']
        for _ in range(randint(5, 8)):
            x = randint(50, 750)
            y = randint(50, 550)
            size = randint(30, 60)

            bubble = self.canvas.create_oval(
                x, y, x + size, y + size,
                fill=choice(colors),
                outline='white',
                width=2
            )
            self.canvas.tag_bind(bubble, '<Button-1>', self.pop_bubble)
            self.move_bubble(bubble)

    def move_bubble(self, bubble):
        """让泡泡随机移动"""
        dx = randint(-self.bubble_speed, self.bubble_speed)
        dy = randint(-self.bubble_speed, self.bubble_speed)
        self.canvas.move(bubble, dx, dy)
        self.root.after(50, lambda: self.move_bubble(bubble))

    def pop_bubble(self, event):
        """点击泡泡时的效果"""
        self.canvas.delete(self.canvas.find_closest(event.x, event.y))
        self.score += 10
        self.score_label.config(text=f"分数: {self.score}")
        self.create_bubbles()  # 补充被点击掉的泡泡

    def update_timer(self):
        """更新游戏计时"""
        if self.time_left > 0:
            self.time_left -= 1
            self.time_label.config(text=f"剩余时间: {self.time_left}")
            self.root.after(1000, self.update_timer)
        else:
            self.game_over()

    def game_over(self):
        """游戏结束界面"""
        self.canvas.delete("all")
        self.canvas.create_text(400, 250,
                                text=f"游戏结束！最终得分: {self.score}",
                                font=('Arial', 24),
                                fill='purple'
                                )
        restart_btn = tk.Button(self.root, text="重新开始", command=self.restart_game)
        self.canvas.create_window(400, 350, window=restart_btn)

    def restart_game(self):
        """重置游戏"""
        self.canvas.delete("all")
        self.score = 0
        self.time_left = 30
        self.score_label.config(text=f"分数: {self.score}")
        self.time_label.config(text=f"剩余时间: {self.time_left}")
        self.create_bubbles()
        self.update_timer()


if __name__ == "__main__":
    root = tk.Tk()
    game = BubbleShooter(root)
    root.mainloop()